#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cmath>

std::vector<int> getInts(std::vector<std::string> a) {
  std::vector<int> ints({stoi(a[0], 0, 16),stoi(a[1], 0, 16),stoi(a[2], 0, 16)});
  return ints;
}

std::vector<std::string> splitArg(std::string a) {
  std::vector<std::string> result;
  for(int i = 0; i < 3; i++) {
    result.push_back(a.substr(i*2,2));
  }
  return result;
}

std::string getHex(std::vector<int> a) {
    std::string result;
    for(int i = 0; i < 3; i++) {
    	std::stringstream ss;
	ss << std::hex << a[i];
	if(a[i] < 16) {
	    result.append(std::string("0") + ss.str());
	    continue;
	}
	result.append(ss.str());
    }
    return result;
}

int clamp(int a, int min, int max) {
  if (a < min) {
    return min;
  } else if(a > max) {
    return max;
  } else {
    return a;
  }
}

std::vector<int> shiftHue(std::vector<int> color, float hue) {
  std::vector<int> result;
  float cosA = std::cos(hue*M_PI/180);
  float sinA = std::sin(hue*M_PI/180);
  float matrix[3][3];
  matrix[0][0] = cosA + (1.0 - cosA) / 3.0;
  matrix[0][1] = 1./3. * (1.0 - cosA) - sqrt(1./3.) * sinA;
  matrix[0][2] = 1./3. * (1.0 - cosA) + sqrt(1./3.) * sinA;
  matrix[1][0] = 1./3. * (1.0 - cosA) + sqrt(1./3.) * sinA;
  matrix[1][1] = cosA + 1./3.*(1.0 - cosA);
  matrix[1][2] = 1./3. * (1.0 - cosA) - sqrt(1./3.) * sinA;
  matrix[2][0] = 1./3. * (1.0 - cosA) - sqrt(1./3.) * sinA;
  matrix[2][1] = 1./3. * (1.0 - cosA) + sqrt(1./3.) * sinA;
  matrix[2][2] = cosA + 1./3. * (1.0 - cosA);
  for(int i = 0; i < 3; i++) {
    result.push_back(clamp(std::round(color[0] * matrix[i][0] + color[1] * matrix[i][1] + color[2] * matrix[i][2]), 0, 255));
  }
  return result;

}

int main(int argc, char *argv[]) {
  if(argc < 4) {
    return 1;
  } else {
    auto arr = getInts(splitArg(std::string(argv[1])));
    float n = std::stof(argv[3]);
    if (std::string("/") == std::string(argv[2])) {
      for(int i = 0; i < 3; i++) {
	arr[i] = clamp((int)std::round(arr[i] / n), 0, 255);
      }
    } else if (std::string("*") == std::string(argv[2])) {
      for(int i = 0; i < 3; i++) {
	arr[i] = clamp((int)std::round(arr[i] * n), 0, 255);
      }
    } else if (std::string("hue") == std::string(argv[2])) {
      arr = shiftHue(arr, std::stof(argv[3]));
    }

    std::cout << getHex(arr) << "\n";

  }
  return 0;
}
